<?php

namespace App\Admin\Controllers;

use App\Models\Club;
use App\Models\SchoolClassAndGrade;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class SchoolClassAndGradeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'SchoolClassAndGrade';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SchoolClassAndGrade());

        $grid->column('id', __('Id'));
        $grid->column('grade', __('年级'));
        $grid->column('class_name', __('班级'));
        $grid->column('status', __('状态'))->display(function ($status) {
            $text = $status === 1?'启用':'禁用';
            return "<span>$text</span>";
        });
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->disableColumnSelector();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SchoolClassAndGrade::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('grade', __('年级'));
        $show->field('class_name', __('班级'));
        $show->field('status', __('状态'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SchoolClassAndGrade());

        $form->text('grade', __('年级'));
        $form->text('class_name', __('班级'));
        $form->radio('status','状态')->options([1=>'启用',2=>'禁用']);
        $form->select('club_id','社团班级')->options(Club::all()->pluck('club_name','id'));
        $form->hasMany('class_and_grade_time','上课时间', function (Form\NestedForm $form) {
            $form->select('week_text','星期')->options([
                '星期一'=>'星期一',
                '星期二'=>'星期二',
                '星期三'=>'星期三',
                '星期四'=>'星期四',
                '星期五'=>'星期五',
                '星期六'=>'星期六',
                '星期天'=>'星期天',
            ]);
            $form->time('start_time','上课时间')->format('HH:mm:ss');;
            $form->time('end_time','下课时间')->format('HH:mm:ss');;
            $form->number('max_num','人数');
        });
        $form->tools(function (Form\Tools $tools) {

            // 去掉`列表`按钮
            $tools->disableList();

            // 去掉`删除`按钮
            $tools->disableDelete();

            // 去掉`查看`按钮
            $tools->disableView();

            $tools->add('<a class="btn btn-sm btn-default" onclick="window.history.go(-1);"></i>&nbsp;&nbsp;返回</a>');
        });
        $form->footer(function (Form\Footer $footer) {

            // 去掉`重置`按钮
            $footer->disableReset();

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });
        $form->saved(function (Form $form) {
            return redirect('admin/schools/'.$form->model()->school_id);
        });
        return $form;
    }
}
